<?php
	class Generic_table extends Model{
		function Generic_table(){
			parent::Model();
		}
		
		function get_option_array(&$options, $table, $value_field, $disp_field, $where = null, $order_by = null){
			$this->db->select( "$value_field, $disp_field");
			if( isset($where) && !empty($where)){
				$this->db->where($where);
			}
			if( !empty($order_by) ){
				$this->db->order_by($order_by);
			}
			$query = $this->db->get($table);
			foreach( $query->result() as $row){
				$options[$row->$value_field] = $row->$disp_field;
			}
		}
		
		function get_field_where($table, $field, $ret_field, $cvalue, $ctable = null, $cfield = null){
			$this->db->select("$ret_field as ret_field");
			$this->db->from($table);
			$this->db->where("$field = $cvalue");
			$this->db->limit(1);
			$query = $this->db->get()->row_array(1);
			if( $query ){
				return $query['ret_field'];
			}else{
				return null;
			}
		}
		
		function get_fields_where($table, $field, $ret_field, $cvalue, $ctable = null, $cfield = null){
			$this->db->select("$ret_field as ret_field");
			$this->db->from($table);
			$this->db->where("$field = $cvalue");
			$arr = array();
			$ret = $this->db->get()->result_array();
			if( $ret && count($ret)){
				foreach( $ret as $row){
					$arr[] = $row['ret_field'];
				}
			}
			return $arr;
		}
		
		function get_current_images(&$selected, $table, $id){
			$query = $this->db->query("SELECT image_id FROM image_relations WHERE rel_id = " . $id . " AND table_name = '" . $table . "'")->result_array();
			foreach( $query as $row){
				$selected[] = $row['image_id'];
			}
		}
	}
?>